package csm.uttils.windows;

import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CatchUpOptionPane.java
 *
 * Created on 07/10/2009, 21:33:51
 */

/**
 *
 * @author Carlos S. Moreno
 */
public class CatchUpOptionPane extends javax.swing.JDialog {

    //Crea la variable del codigo de confirmacion y genera un codigo nuevo.
    private String codigo = csm.uttils.Operations.generateCode(4,true);
    private int answer = JOptionPane.CANCEL_OPTION;

    private void generarCodigo() {
        lblCodigo.setText(String.valueOf(codigo));
    }

    public CatchUpOptionPane(java.awt.Frame parent, boolean modal)
    {
        this(parent, "Confirmar", modal);
    }

    public CatchUpOptionPane(java.awt.Frame parent, String title)
    {
        this(parent, title, true);
    }

    /** Creates new form CatchUpOptionPane */
    public CatchUpOptionPane(java.awt.Frame parent,String title, boolean modal) {
        this(parent, title, modal, "¿Desea continuar con la operacion?");
    }

    /** Creates new form CatchUpOptionPane */
    public CatchUpOptionPane(java.awt.Frame parent,String title, boolean modal, String message) {
        super(parent, title, modal);
        initComponents();
        lblMensaje.setText(message);
        int width = (int)(lblMensaje.getSize().getWidth()) + 380;
        int height = (int)(centerPanel.getSize().getHeight()) + 75;
        this.setSize(width, height);
        generarCodigo();
    }

    public void setMessage(String message)
    {
        lblMensaje.setText(message);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        centerPanel = new javax.swing.JPanel();
        lblMensaje = new javax.swing.JLabel();
        lblIngresar = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        lblCodSeg = new javax.swing.JLabel();
        lblCodigo = new javax.swing.JLabel();
        leftPanel = new javax.swing.JPanel();
        lblAlert = new javax.swing.JLabel();
        bottomPanel = new javax.swing.JPanel();
        btnConfirmar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setTitle("Confirmación Requerida");
        setModal(true);

        centerPanel.setMaximumSize(new java.awt.Dimension(548, 140));
        centerPanel.setPreferredSize(new java.awt.Dimension(248, 40));

        lblMensaje.setText("¿Confirma?");

        lblIngresar.setText("Ingrese el código:");

        txtCodigo.setFont(new java.awt.Font("Lucida Grande", 1, 14));
        txtCodigo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtCodigoKeyTyped(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtCodigoKeyReleased(evt);
            }
        });

        lblCodSeg.setText("Código de Seguridad:");

        lblCodigo.setFont(new java.awt.Font("Lucida Grande", 1, 14));
        lblCodigo.setForeground(new java.awt.Color(200, 85, 12));
        lblCodigo.setText("XXXX");

        javax.swing.GroupLayout centerPanelLayout = new javax.swing.GroupLayout(centerPanel);
        centerPanel.setLayout(centerPanelLayout);
        centerPanelLayout.setHorizontalGroup(
            centerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(centerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(centerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(centerPanelLayout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addGroup(centerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblIngresar)
                            .addComponent(lblCodSeg))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(centerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblCodigo)))
                    .addComponent(lblMensaje))
                .addContainerGap(264, Short.MAX_VALUE))
        );
        centerPanelLayout.setVerticalGroup(
            centerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(centerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblMensaje)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(centerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCodSeg, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblCodigo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(centerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblIngresar, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        getContentPane().add(centerPanel, java.awt.BorderLayout.CENTER);

        leftPanel.setMaximumSize(new java.awt.Dimension(70, 80));
        leftPanel.setLayout(new java.awt.GridBagLayout());

        lblAlert.setIcon(new javax.swing.ImageIcon(getClass().getResource("/uttils/img/catchup.png"))); // NOI18N
        leftPanel.add(lblAlert, new java.awt.GridBagConstraints());

        getContentPane().add(leftPanel, java.awt.BorderLayout.LINE_START);

        bottomPanel.setLayout(new java.awt.GridBagLayout());

        btnConfirmar.setText("Confirmar");
        btnConfirmar.setEnabled(false);
        btnConfirmar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConfirmarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(7, 4, 7, 4);
        bottomPanel.add(btnConfirmar, gridBagConstraints);

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.insets = new java.awt.Insets(7, 4, 7, 4);
        bottomPanel.add(btnCancelar, gridBagConstraints);

        getContentPane().add(bottomPanel, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnConfirmarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfirmarActionPerformed
        answer = JOptionPane.OK_OPTION;
        this.setEnabled(false);
        this.setVisible(false);
    }//GEN-LAST:event_btnConfirmarActionPerformed

    public int getAnswer(){return answer;}

    private void txtCodigoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCodigoKeyTyped
        
    }//GEN-LAST:event_txtCodigoKeyTyped

    private void txtCodigoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCodigoKeyReleased
        btnConfirmar.setEnabled(txtCodigo.getText().compareToIgnoreCase(lblCodigo.getText())==0);        
    }//GEN-LAST:event_txtCodigoKeyReleased

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        answer = JOptionPane.CANCEL_OPTION;
        this.setEnabled(false);
        this.setVisible(false);
    }//GEN-LAST:event_btnCancelarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                CatchUpOptionPane dialog = new CatchUpOptionPane(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel bottomPanel;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnConfirmar;
    private javax.swing.JPanel centerPanel;
    private javax.swing.JLabel lblAlert;
    private javax.swing.JLabel lblCodSeg;
    private javax.swing.JLabel lblCodigo;
    private javax.swing.JLabel lblIngresar;
    private javax.swing.JLabel lblMensaje;
    private javax.swing.JPanel leftPanel;
    private javax.swing.JTextField txtCodigo;
    // End of variables declaration//GEN-END:variables

}
